#include <iostream>
#include <vector>
#include <string>
using namespace std;
//观察者模式
//行为设计模式，它定义了一种一对多的依赖关系，
//让多个观察者对象同时监听一个主题者对象，当主题对象的状态发生变化时
//会自动通知观察者对象自动更新自己的状态

class Observer
{
public:
	Observer()
	{}
	virtual ~Observer()  //防止特定情况下出现内存泄露
	{}
	virtual void update(float _temprature, string _weatherCondition) = 0;//约束子类必须重写该虚函数
};

class WeatherData
{
public:
	WeatherData(float _temprature, string _weatherCondition)
		:temprature(_temprature)
		, weatherCondition(_weatherCondition)
	{}

	void attach(Observer* observer)
	{
		observers.push_back(observer);
	}

	void setWeatherData(float _temprature, string _weathercondition)
	{
		temprature = _temprature;
		weatherCondition = _weathercondition;
	}

	void notify()
	{
		for (auto observer : observers)
		{
			observer->update(temprature, weatherCondition);
		}
	}

private:
	float temprature;
	string weatherCondition;
	vector<Observer*> observers;
};

class WeatherTempratureDisplay : public Observer
{
public:
	WeatherTempratureDisplay(WeatherData* weatherdate)
	{
		weatherdate->attach(this); //创建观察者对象时，将该观察者对象加入到主题对象中
	}
	virtual void update(float _temprature, string _weatherCondition) override
	{
		this->temprature = _temprature;
		display();
	}
	void display()
	{
		printf("temprature is : %f\n", temprature);
	}
private:
	float temprature;
};
//
class WeatherConditionDisplay : public Observer
{
public:
	WeatherConditionDisplay(WeatherData* weatherdate)
	{
		weatherdate->attach(this); //创建观察者对象时，将该观察者对象加入到主题对象中
	}
	virtual void update(float _temprature, string _weatherCondition) override  //倒逼子类对象对继承的父类对象中的虚函数进行重写
	{
		this->weatherCondition = _weatherCondition;
		display();
	}
	void display()
	{
		cout << "weatherCondtion is : " << weatherCondition << endl;
	}
private:
	string weatherCondition;
};

int main()
{
	return 0;
}